//
//  BMHYearMonthDatePickerView.m
//  
//
//  Created by 陈宪栋 on 2017/6/15.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "BMHYearMonthDatePickerView.h"
#import <UtilsMacro.h>
#import <Masonry.h>
#import <ReactiveCocoa.h>
#import "XLHTTPSessionManager.h"

@interface BMHYearMonthDatePickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) NSMutableArray *yearArray;
@property (nonatomic, strong) NSMutableArray *monthArray;
@property (nonatomic, strong) NSString *selectedMonth;
@property (nonatomic, strong) NSString *selectedYear;
@property (nonatomic, assign) NSInteger lastSelectedMonthRow;
@property (nonatomic, copy) NSString *currentYear;
@property (nonatomic, copy) NSString *currentMonth;
@end

@implementation BMHYearMonthDatePickerView
- (NSMutableArray *)yearArray {
    if (_yearArray == nil) {
        _yearArray = [NSMutableArray array];
        
    }
    return _yearArray;
}
- (NSMutableArray *)monthArray {
    if (_monthArray == nil) {
        _monthArray = [NSMutableArray array];
        
    }
    return _monthArray;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    

    
    
    
    
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    [formatter setDateFormat:@"yyyy"];
    NSInteger currentYear = [[formatter stringFromDate:currentDate] integerValue];
    [formatter setDateFormat:@"MM"];
    NSInteger currentMonth = [[formatter stringFromDate:currentDate] integerValue];
    NSInteger originYear = 1999;
    NSLog(@"fssssss == %ld ==== %ld", originYear, currentYear);
    for (NSInteger i = originYear; i <= currentYear; i++) {
        [self.yearArray addObject:F(@"%zd", i)];
    }
    for (NSInteger i = 1; i <= 12; i++) {
        [self.monthArray addObject:F(@"%zd", i)];
    }
    NSLog(@"yeadddd == %@",self.yearArray);
    self.currentYear = self.yearArray[currentYear - originYear];
    self.currentMonth = self.monthArray[currentMonth - 1];
    self.selectedYear = self.yearArray[currentYear - originYear];
    self.selectedMonth = self.monthArray[currentMonth - 1];

    UIPickerView *datePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, ScreenWidth, 200)];
    datePicker.backgroundColor = [UIColor whiteColor];
    datePicker.delegate = self;
    datePicker.dataSource = self;
    [datePicker selectRow:[self.yearArray indexOfObject:self.selectedYear] inComponent:0 animated:NO];
    [datePicker selectRow:[self.monthArray indexOfObject:self.selectedMonth] + 12 * 10 inComponent:1 animated:NO];
    self.lastSelectedMonthRow = [self.monthArray indexOfObject:self.selectedMonth] + 12 * 10;
    [self addSubview:datePicker];
    [datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    

    UIView *optionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, XQScreenW, 44)];
    optionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:optionView];
    
//    UIButton *cancleBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 88, optionView.height)];
//    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
//    [cancleBtn setTitleColor:RGB(199, 199, 205) forState:UIControlStateNormal];
//    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:18];
//    @weakify(self);
//    [[cancleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
//        @strongify(self);
//        [self dismiss];
//    }];
//    [optionView addSubview:cancleBtn];
     @weakify(self);
    UIButton *sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(optionView.width - 88, 0, 88, optionView.height)];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        [self.sureSignal sendNext:[NSString stringWithFormat:@"%@-%@",self.selectedYear,self.selectedMonth]];
        [self dismiss];
    }];
    [sureBtn addTarget:self action:@selector(didSureBtn:) forControlEvents:UIControlEventTouchUpInside];
    [optionView addSubview:sureBtn];

}
- (void)didSureBtn:(UIButton *)sender {
    if ([self.selectedMonth integerValue] < 10) {
        self.selectedMonth =[NSString stringWithFormat:@"0%@",self.selectedMonth];
    }else {
        self.selectedMonth =[NSString stringWithFormat:@"%@",self.selectedMonth];
        
        
    }

    self.selectedYearMonth(self.selectedYear, self.selectedMonth);

}
- (void)dismiss {
    [self.dismissSignal sendNext:nil];
    //
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.yearArray.count;
    } else {
        return self.monthArray.count * self.yearArray.count;
    }
}

#pragma mark - UIPickerViewDelegate
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        return F(@"%@年", self.yearArray[row]);
    } else {
        return F(@"%@月", self.monthArray[row % 12]);
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.selectedYear = self.yearArray[row];
    } else {
        self.selectedMonth = self.monthArray[row % 12];
        self.lastSelectedMonthRow = row;
    }
    NSInteger selectedMonth = self.selectedMonth.integerValue;
    NSInteger currentMonth = self.currentMonth.integerValue;
    
    if ([self.selectedYear isEqualToString:self.currentYear] && currentMonth < selectedMonth) {
        [pickerView selectRow:self.lastSelectedMonthRow - (selectedMonth - currentMonth) inComponent:1 animated:YES];
        self.selectedMonth = self.monthArray[self.lastSelectedMonthRow % 12 - (selectedMonth - currentMonth)];
        if (self.selectedYearMonth) {
//            if ([self.selectedMonth integerValue] < 10) {
//                self.selectedMonth =[NSString stringWithFormat:@"0%@",self.selectedMonth];
//            }else {
//                self.selectedMonth =[NSString stringWithFormat:@"%@",self.selectedMonth];
//
//
//            }
        }
        NSLog(@"%@", F(@"%@年%@月账单", self.selectedYear, self.selectedMonth));
        return;
    }
    if ([self.selectedYear isEqualToString:self.currentYear] && [self.selectedMonth isEqualToString:self.currentMonth]) {
        NSLog(@"%@", F(@"%@年%@月账单", self.selectedYear, self.selectedMonth));
        if (self.selectedYearMonth) {
//            if ([self.selectedMonth integerValue] < 10) {
//                self.selectedMonth =[NSString stringWithFormat:@"0%@",self.selectedMonth];
//            }else {
//                self.selectedMonth =[NSString stringWithFormat:@"%@",self.selectedMonth];
//                
//                
//            }
        }
    } else {
        NSLog(@"%@", F(@"%@年%@月账单", self.selectedYear, self.selectedMonth));
        if (self.selectedYearMonth) {
//            if ([self.selectedMonth integerValue] < 10) {
//                self.selectedMonth =[NSString stringWithFormat:@"0%@",self.selectedMonth];
//            }else {
//                self.selectedMonth =[NSString stringWithFormat:@"%@",self.selectedMonth];
//                
//                
//            }
        }
    }
}

@end
